package cn.z2huo.demo.spring.transactional.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronizationManager;

/**
 * <p>声明式事务的 isActualTransactionActive 方法测试
 *
 * @author z2huo
 */
@Slf4j
@Service
public class InTransactionTestService {

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void transactionAnnotation() {
        log.info("transactionAnnotation method");

        boolean actualTransactionActive = TransactionSynchronizationManager.isActualTransactionActive();
        if (actualTransactionActive) {
            log.info("transaction exists");
        }

        Integer currentTransactionIsolationLevel = TransactionSynchronizationManager.getCurrentTransactionIsolationLevel();
        log.info("currentTransactionIsolationLevel is {}", currentTransactionIsolationLevel);
    }

    public void noTransactionAnnotation() {
        log.info("noTransactionAnnotation method");

        boolean actualTransactionActive = TransactionSynchronizationManager.isActualTransactionActive();
        if (actualTransactionActive) {
            log.info("transaction exists");
        }
    }

}
